
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 06/27/2011 19:45:46
-- Generated from EDMX file: E:\Hoc Tap\Thuc hanh\Visual Studio C#\NMCNPM\De tai QLTV\Src\QLTV\DAO\Model1.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [QLTV];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_CT_PHIEUMUON_PHIEU_MUON]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CT_PHIEUMUON] DROP CONSTRAINT [FK_CT_PHIEUMUON_PHIEU_MUON];
GO
IF OBJECT_ID(N'[dbo].[FK_CT_PHIEUMUON_SACH]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CT_PHIEUMUON] DROP CONSTRAINT [FK_CT_PHIEUMUON_SACH];
GO
IF OBJECT_ID(N'[dbo].[FK_CT_PHIEUTRA_CT_PHIEUMUON]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CT_PHIEUTRA] DROP CONSTRAINT [FK_CT_PHIEUTRA_CT_PHIEUMUON];
GO
IF OBJECT_ID(N'[dbo].[FK_CT_PHIEUTRA_PHIEU_TRA]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CT_PHIEUTRA] DROP CONSTRAINT [FK_CT_PHIEUTRA_PHIEU_TRA];
GO
IF OBJECT_ID(N'[dbo].[FK_CT_THANHLY_LYDO_THANHLY]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CT_THANHLY] DROP CONSTRAINT [FK_CT_THANHLY_LYDO_THANHLY];
GO
IF OBJECT_ID(N'[dbo].[FK_CT_THANHLY_SACH]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CT_THANHLY] DROP CONSTRAINT [FK_CT_THANHLY_SACH];
GO
IF OBJECT_ID(N'[dbo].[FK_CT_THANHLY_THANH_LY_SACH]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CT_THANHLY] DROP CONSTRAINT [FK_CT_THANHLY_THANH_LY_SACH];
GO
IF OBJECT_ID(N'[dbo].[FK_DOC_GIA_LOAI_DOCGIA]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[DOC_GIA] DROP CONSTRAINT [FK_DOC_GIA_LOAI_DOCGIA];
GO
IF OBJECT_ID(N'[dbo].[FK_DOC_GIA_NHAN_VIEN]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[DOC_GIA] DROP CONSTRAINT [FK_DOC_GIA_NHAN_VIEN];
GO
IF OBJECT_ID(N'[dbo].[FK_NHAN_VIEN_BANG_CAP]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[NHAN_VIEN] DROP CONSTRAINT [FK_NHAN_VIEN_BANG_CAP];
GO
IF OBJECT_ID(N'[dbo].[FK_NHAN_VIEN_BO_PHAN]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[NHAN_VIEN] DROP CONSTRAINT [FK_NHAN_VIEN_BO_PHAN];
GO
IF OBJECT_ID(N'[dbo].[FK_NHAN_VIEN_CHUC_VU]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[NHAN_VIEN] DROP CONSTRAINT [FK_NHAN_VIEN_CHUC_VU];
GO
IF OBJECT_ID(N'[dbo].[FK_PHIEU_MUON_DOC_GIA]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PHIEU_MUON] DROP CONSTRAINT [FK_PHIEU_MUON_DOC_GIA];
GO
IF OBJECT_ID(N'[dbo].[FK_PHIEU_TRA_DOC_GIA]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PHIEU_TRA] DROP CONSTRAINT [FK_PHIEU_TRA_DOC_GIA];
GO
IF OBJECT_ID(N'[dbo].[FK_PHIEUGHI_MATSACH_DOC_GIA]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PHIEUGHI_MATSACH] DROP CONSTRAINT [FK_PHIEUGHI_MATSACH_DOC_GIA];
GO
IF OBJECT_ID(N'[dbo].[FK_PHIEUGHI_MATSACH_NHAN_VIEN]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PHIEUGHI_MATSACH] DROP CONSTRAINT [FK_PHIEUGHI_MATSACH_NHAN_VIEN];
GO
IF OBJECT_ID(N'[dbo].[FK_PHIEUGHI_MATSACH_SACH]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PHIEUGHI_MATSACH] DROP CONSTRAINT [FK_PHIEUGHI_MATSACH_SACH];
GO
IF OBJECT_ID(N'[dbo].[FK_PHIEUTHU_TIENPHAT_DOC_GIA]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PHIEUTHU_TIENPHAT] DROP CONSTRAINT [FK_PHIEUTHU_TIENPHAT_DOC_GIA];
GO
IF OBJECT_ID(N'[dbo].[FK_PHIEUTHU_TIENPHAT_NHAN_VIEN]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PHIEUTHU_TIENPHAT] DROP CONSTRAINT [FK_PHIEUTHU_TIENPHAT_NHAN_VIEN];
GO
IF OBJECT_ID(N'[dbo].[FK_SACH_NHA_XUAT_BAN]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SACH] DROP CONSTRAINT [FK_SACH_NHA_XUAT_BAN];
GO
IF OBJECT_ID(N'[dbo].[FK_SACH_NHAN_VIEN]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SACH] DROP CONSTRAINT [FK_SACH_NHAN_VIEN];
GO
IF OBJECT_ID(N'[dbo].[FK_SACH_TAC_GIA]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SACH] DROP CONSTRAINT [FK_SACH_TAC_GIA];
GO
IF OBJECT_ID(N'[dbo].[FK_SACH_THE_LOAI]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SACH] DROP CONSTRAINT [FK_SACH_THE_LOAI];
GO
IF OBJECT_ID(N'[dbo].[FK_SACH_TINH_TRANG]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SACH] DROP CONSTRAINT [FK_SACH_TINH_TRANG];
GO
IF OBJECT_ID(N'[dbo].[FK_THANH_LY_SACH_NHAN_VIEN]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[THANH_LY_SACH] DROP CONSTRAINT [FK_THANH_LY_SACH_NHAN_VIEN];
GO
IF OBJECT_ID(N'[dbo].[FK_USER_NHAN_VIEN]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[USER] DROP CONSTRAINT [FK_USER_NHAN_VIEN];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[BANG_CAP]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BANG_CAP];
GO
IF OBJECT_ID(N'[dbo].[BO_PHAN]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BO_PHAN];
GO
IF OBJECT_ID(N'[dbo].[CHUC_VU]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CHUC_VU];
GO
IF OBJECT_ID(N'[dbo].[CT_PHIEUMUON]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CT_PHIEUMUON];
GO
IF OBJECT_ID(N'[dbo].[CT_PHIEUTRA]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CT_PHIEUTRA];
GO
IF OBJECT_ID(N'[dbo].[CT_THANHLY]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CT_THANHLY];
GO
IF OBJECT_ID(N'[dbo].[DOC_GIA]', 'U') IS NOT NULL
    DROP TABLE [dbo].[DOC_GIA];
GO
IF OBJECT_ID(N'[dbo].[LOAI_DOCGIA]', 'U') IS NOT NULL
    DROP TABLE [dbo].[LOAI_DOCGIA];
GO
IF OBJECT_ID(N'[dbo].[LYDO_THANHLY]', 'U') IS NOT NULL
    DROP TABLE [dbo].[LYDO_THANHLY];
GO
IF OBJECT_ID(N'[dbo].[NHA_XUAT_BAN]', 'U') IS NOT NULL
    DROP TABLE [dbo].[NHA_XUAT_BAN];
GO
IF OBJECT_ID(N'[dbo].[NHAN_VIEN]', 'U') IS NOT NULL
    DROP TABLE [dbo].[NHAN_VIEN];
GO
IF OBJECT_ID(N'[dbo].[PHIEU_MUON]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PHIEU_MUON];
GO
IF OBJECT_ID(N'[dbo].[PHIEU_TRA]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PHIEU_TRA];
GO
IF OBJECT_ID(N'[dbo].[PHIEUGHI_MATSACH]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PHIEUGHI_MATSACH];
GO
IF OBJECT_ID(N'[dbo].[PHIEUTHU_TIENPHAT]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PHIEUTHU_TIENPHAT];
GO
IF OBJECT_ID(N'[dbo].[SACH]', 'U') IS NOT NULL
    DROP TABLE [dbo].[SACH];
GO
IF OBJECT_ID(N'[dbo].[TAC_GIA]', 'U') IS NOT NULL
    DROP TABLE [dbo].[TAC_GIA];
GO
IF OBJECT_ID(N'[dbo].[THAM_SO]', 'U') IS NOT NULL
    DROP TABLE [dbo].[THAM_SO];
GO
IF OBJECT_ID(N'[dbo].[THANH_LY_SACH]', 'U') IS NOT NULL
    DROP TABLE [dbo].[THANH_LY_SACH];
GO
IF OBJECT_ID(N'[dbo].[THE_LOAI]', 'U') IS NOT NULL
    DROP TABLE [dbo].[THE_LOAI];
GO
IF OBJECT_ID(N'[dbo].[TINH_TRANG]', 'U') IS NOT NULL
    DROP TABLE [dbo].[TINH_TRANG];
GO
IF OBJECT_ID(N'[dbo].[USER]', 'U') IS NOT NULL
    DROP TABLE [dbo].[USER];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'BANG_CAP'
CREATE TABLE [dbo].[BANG_CAP] (
    [MaLoaiBangCap] int IDENTITY(1,1) NOT NULL,
    [TenLoaiBangCap] nvarchar(50)  NOT NULL
);
GO

-- Creating table 'BO_PHANList'
CREATE TABLE [dbo].[BO_PHANList] (
    [MaBoPhan] int IDENTITY(1,1) NOT NULL,
    [TenBoPhan] nvarchar(50)  NULL
);
GO

-- Creating table 'CHUC_VU'
CREATE TABLE [dbo].[CHUC_VU] (
    [MaChucVu] int IDENTITY(1,1) NOT NULL,
    [TenChucVu] nvarchar(50)  NOT NULL
);
GO

-- Creating table 'CT_PHIEUMUONList'
CREATE TABLE [dbo].[CT_PHIEUMUONList] (
    [MaCT_PhieuMuon] int IDENTITY(1,1) NOT NULL,
    [MaPhieuMuon] int  NOT NULL,
    [MaSach] int  NOT NULL
);
GO

-- Creating table 'CT_PHIEUTRAList'
CREATE TABLE [dbo].[CT_PHIEUTRAList] (
    [MaCT_PhieuTra] int IDENTITY(1,1) NOT NULL,
    [MaPhieuTra] int  NOT NULL,
    [MaCT_PhieuMuon] int  NOT NULL,
    [TienPhat] float  NOT NULL
);
GO

-- Creating table 'CT_THANHLY'
CREATE TABLE [dbo].[CT_THANHLY] (
    [MaCT_ThanhLy] int IDENTITY(1,1) NOT NULL,
    [MaPhieuThanhLy] int  NOT NULL,
    [MaSach] int  NOT NULL,
    [MaLyDo] int  NOT NULL
);
GO

-- Creating table 'DOC_GIAList'
CREATE TABLE [dbo].[DOC_GIAList] (
    [MaTheDocGia] int IDENTITY(1,1) NOT NULL,
    [HoTen] nvarchar(50)  NOT NULL,
    [NgaySinh] datetime  NOT NULL,
    [DiaChi] nvarchar(150)  NULL,
    [Email] varchar(50)  NULL,
    [MaLoaiDocGia] int  NOT NULL,
    [NgayLapThe] datetime  NOT NULL,
    [NguoiLap] int  NOT NULL,
    [ThoiHanThe] datetime  NOT NULL,
    [SoSachDangMuon] int  NULL,
    [TienNo] float  NULL,
    [DaXoa] int  NULL
);
GO

-- Creating table 'LOAI_DOCGIAList'
CREATE TABLE [dbo].[LOAI_DOCGIAList] (
    [MaLoaiDocGia] int IDENTITY(1,1) NOT NULL,
    [TenLoaiDocGia] nvarchar(50)  NOT NULL
);
GO

-- Creating table 'LYDO_THANHLY'
CREATE TABLE [dbo].[LYDO_THANHLY] (
    [MaLyDo] int IDENTITY(1,1) NOT NULL,
    [TenLyDo] nvarchar(50)  NOT NULL
);
GO

-- Creating table 'NHA_XUAT_BAN'
CREATE TABLE [dbo].[NHA_XUAT_BAN] (
    [MaNhaXuatBan] int IDENTITY(1,1) NOT NULL,
    [TenNhaXuatBan] nvarchar(150)  NOT NULL,
    [DaXoa] int  NULL
);
GO

-- Creating table 'NHAN_VIENList'
CREATE TABLE [dbo].[NHAN_VIENList] (
    [MaNV] int IDENTITY(1,1) NOT NULL,
    [HoTen] nvarchar(50)  NOT NULL,
    [DiaChi] nvarchar(150)  NULL,
    [NgaySinh] datetime  NOT NULL,
    [DienThoai] int  NULL,
    [LoaiBangCap] int  NOT NULL,
    [BoPhan] int  NOT NULL,
    [ChucVu] int  NOT NULL,
    [DaXoa] int  NULL,
    [MatKhau] varchar(150)  NULL
);
GO

-- Creating table 'PHIEU_MUONList'
CREATE TABLE [dbo].[PHIEU_MUONList] (
    [MaPhieuMuon] int IDENTITY(1,1) NOT NULL,
    [MaTheDocGia] int  NOT NULL,
    [NgayMuon] datetime  NOT NULL,
    [NgayTraToiDa] datetime  NULL
);
GO

-- Creating table 'PHIEU_TRAList'
CREATE TABLE [dbo].[PHIEU_TRAList] (
    [MaPhieuTra] int IDENTITY(1,1) NOT NULL,
    [NgayTra] datetime  NOT NULL,
    [MaTheDocGia] int  NOT NULL
);
GO

-- Creating table 'PHIEUGHI_MATSACH'
CREATE TABLE [dbo].[PHIEUGHI_MATSACH] (
    [MaPhieuGhi] int IDENTITY(1,1) NOT NULL,
    [NgayGhiNhan] datetime  NOT NULL,
    [MaTheDocGia] int  NOT NULL,
    [MaSach] int  NOT NULL,
    [MaNV] int  NOT NULL,
    [TienPhat] float  NOT NULL
);
GO

-- Creating table 'PHIEUTHU_TIENPHAT'
CREATE TABLE [dbo].[PHIEUTHU_TIENPHAT] (
    [MaPhieuThu] int  NOT NULL,
    [MaTheDocGia] int  NOT NULL,
    [SoTienThu] float  NOT NULL,
    [MaNhanVien] int IDENTITY(1,1) NOT NULL
);
GO

-- Creating table 'SACHList'
CREATE TABLE [dbo].[SACHList] (
    [MaSach] int IDENTITY(1,1) NOT NULL,
    [MaSoSach] varchar(30)  NOT NULL,
    [TenSach] nvarchar(50)  NOT NULL,
    [MaTheLoai] int  NOT NULL,
    [MaTacGia] int  NOT NULL,
    [NamXuatBan] int  NULL,
    [NhaXuatBan] int  NOT NULL,
    [DonGia] float  NOT NULL,
    [NgayNhap] datetime  NULL,
    [NguoiTiepNhan] int  NULL,
    [TinhTrangSach] int  NULL,
    [DaXoa] bit  NULL
);
GO

-- Creating table 'TAC_GIAList'
CREATE TABLE [dbo].[TAC_GIAList] (
    [MaTacGia] int IDENTITY(1,1) NOT NULL,
    [TenTacGia] nvarchar(50)  NOT NULL,
    [DaXoa] int  NULL
);
GO

-- Creating table 'THAM_SO'
CREATE TABLE [dbo].[THAM_SO] (
    [MaTS] int IDENTITY(1,1) NOT NULL,
    [TenThamSo] nvarchar(50)  NOT NULL,
    [GiaTri] int  NOT NULL
);
GO

-- Creating table 'THANH_LY_SACH'
CREATE TABLE [dbo].[THANH_LY_SACH] (
    [MaPhieuThanhLy] int IDENTITY(1,1) NOT NULL,
    [NgayThanhLy] datetime  NOT NULL,
    [NguoiThanhLy] int  NOT NULL
);
GO

-- Creating table 'THE_LOAIList'
CREATE TABLE [dbo].[THE_LOAIList] (
    [MaTheLoai] int IDENTITY(1,1) NOT NULL,
    [TenTheLoai] nvarchar(50)  NOT NULL,
    [TenTat] varchar(10)  NULL,
    [DaXoa] int  NULL
);
GO

-- Creating table 'TINH_TRANG'
CREATE TABLE [dbo].[TINH_TRANG] (
    [MaTinhTrang] int IDENTITY(1,1) NOT NULL,
    [TenTinhTrang] nvarchar(50)  NOT NULL
);
GO

-- Creating table 'USERs'
CREATE TABLE [dbo].[USERs] (
    [MaUser] int IDENTITY(1,1) NOT NULL,
    [TenDangNhap] int  NOT NULL,
    [MatKhau] varchar(50)  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [MaLoaiBangCap] in table 'BANG_CAP'
ALTER TABLE [dbo].[BANG_CAP]
ADD CONSTRAINT [PK_BANG_CAP]
    PRIMARY KEY CLUSTERED ([MaLoaiBangCap] ASC);
GO

-- Creating primary key on [MaBoPhan] in table 'BO_PHANList'
ALTER TABLE [dbo].[BO_PHANList]
ADD CONSTRAINT [PK_BO_PHANList]
    PRIMARY KEY CLUSTERED ([MaBoPhan] ASC);
GO

-- Creating primary key on [MaChucVu] in table 'CHUC_VU'
ALTER TABLE [dbo].[CHUC_VU]
ADD CONSTRAINT [PK_CHUC_VU]
    PRIMARY KEY CLUSTERED ([MaChucVu] ASC);
GO

-- Creating primary key on [MaCT_PhieuMuon] in table 'CT_PHIEUMUONList'
ALTER TABLE [dbo].[CT_PHIEUMUONList]
ADD CONSTRAINT [PK_CT_PHIEUMUONList]
    PRIMARY KEY CLUSTERED ([MaCT_PhieuMuon] ASC);
GO

-- Creating primary key on [MaCT_PhieuTra] in table 'CT_PHIEUTRAList'
ALTER TABLE [dbo].[CT_PHIEUTRAList]
ADD CONSTRAINT [PK_CT_PHIEUTRAList]
    PRIMARY KEY CLUSTERED ([MaCT_PhieuTra] ASC);
GO

-- Creating primary key on [MaCT_ThanhLy] in table 'CT_THANHLY'
ALTER TABLE [dbo].[CT_THANHLY]
ADD CONSTRAINT [PK_CT_THANHLY]
    PRIMARY KEY CLUSTERED ([MaCT_ThanhLy] ASC);
GO

-- Creating primary key on [MaTheDocGia] in table 'DOC_GIAList'
ALTER TABLE [dbo].[DOC_GIAList]
ADD CONSTRAINT [PK_DOC_GIAList]
    PRIMARY KEY CLUSTERED ([MaTheDocGia] ASC);
GO

-- Creating primary key on [MaLoaiDocGia] in table 'LOAI_DOCGIAList'
ALTER TABLE [dbo].[LOAI_DOCGIAList]
ADD CONSTRAINT [PK_LOAI_DOCGIAList]
    PRIMARY KEY CLUSTERED ([MaLoaiDocGia] ASC);
GO

-- Creating primary key on [MaLyDo] in table 'LYDO_THANHLY'
ALTER TABLE [dbo].[LYDO_THANHLY]
ADD CONSTRAINT [PK_LYDO_THANHLY]
    PRIMARY KEY CLUSTERED ([MaLyDo] ASC);
GO

-- Creating primary key on [MaNhaXuatBan] in table 'NHA_XUAT_BAN'
ALTER TABLE [dbo].[NHA_XUAT_BAN]
ADD CONSTRAINT [PK_NHA_XUAT_BAN]
    PRIMARY KEY CLUSTERED ([MaNhaXuatBan] ASC);
GO

-- Creating primary key on [MaNV] in table 'NHAN_VIENList'
ALTER TABLE [dbo].[NHAN_VIENList]
ADD CONSTRAINT [PK_NHAN_VIENList]
    PRIMARY KEY CLUSTERED ([MaNV] ASC);
GO

-- Creating primary key on [MaPhieuMuon] in table 'PHIEU_MUONList'
ALTER TABLE [dbo].[PHIEU_MUONList]
ADD CONSTRAINT [PK_PHIEU_MUONList]
    PRIMARY KEY CLUSTERED ([MaPhieuMuon] ASC);
GO

-- Creating primary key on [MaPhieuTra] in table 'PHIEU_TRAList'
ALTER TABLE [dbo].[PHIEU_TRAList]
ADD CONSTRAINT [PK_PHIEU_TRAList]
    PRIMARY KEY CLUSTERED ([MaPhieuTra] ASC);
GO

-- Creating primary key on [MaPhieuGhi] in table 'PHIEUGHI_MATSACH'
ALTER TABLE [dbo].[PHIEUGHI_MATSACH]
ADD CONSTRAINT [PK_PHIEUGHI_MATSACH]
    PRIMARY KEY CLUSTERED ([MaPhieuGhi] ASC);
GO

-- Creating primary key on [MaPhieuThu] in table 'PHIEUTHU_TIENPHAT'
ALTER TABLE [dbo].[PHIEUTHU_TIENPHAT]
ADD CONSTRAINT [PK_PHIEUTHU_TIENPHAT]
    PRIMARY KEY CLUSTERED ([MaPhieuThu] ASC);
GO

-- Creating primary key on [MaSach] in table 'SACHList'
ALTER TABLE [dbo].[SACHList]
ADD CONSTRAINT [PK_SACHList]
    PRIMARY KEY CLUSTERED ([MaSach] ASC);
GO

-- Creating primary key on [MaTacGia] in table 'TAC_GIAList'
ALTER TABLE [dbo].[TAC_GIAList]
ADD CONSTRAINT [PK_TAC_GIAList]
    PRIMARY KEY CLUSTERED ([MaTacGia] ASC);
GO

-- Creating primary key on [MaTS] in table 'THAM_SO'
ALTER TABLE [dbo].[THAM_SO]
ADD CONSTRAINT [PK_THAM_SO]
    PRIMARY KEY CLUSTERED ([MaTS] ASC);
GO

-- Creating primary key on [MaPhieuThanhLy] in table 'THANH_LY_SACH'
ALTER TABLE [dbo].[THANH_LY_SACH]
ADD CONSTRAINT [PK_THANH_LY_SACH]
    PRIMARY KEY CLUSTERED ([MaPhieuThanhLy] ASC);
GO

-- Creating primary key on [MaTheLoai] in table 'THE_LOAIList'
ALTER TABLE [dbo].[THE_LOAIList]
ADD CONSTRAINT [PK_THE_LOAIList]
    PRIMARY KEY CLUSTERED ([MaTheLoai] ASC);
GO

-- Creating primary key on [MaTinhTrang] in table 'TINH_TRANG'
ALTER TABLE [dbo].[TINH_TRANG]
ADD CONSTRAINT [PK_TINH_TRANG]
    PRIMARY KEY CLUSTERED ([MaTinhTrang] ASC);
GO

-- Creating primary key on [MaUser] in table 'USERs'
ALTER TABLE [dbo].[USERs]
ADD CONSTRAINT [PK_USERs]
    PRIMARY KEY CLUSTERED ([MaUser] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [LoaiBangCap] in table 'NHAN_VIENList'
ALTER TABLE [dbo].[NHAN_VIENList]
ADD CONSTRAINT [FK_NHAN_VIEN_BANG_CAP]
    FOREIGN KEY ([LoaiBangCap])
    REFERENCES [dbo].[BANG_CAP]
        ([MaLoaiBangCap])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_NHAN_VIEN_BANG_CAP'
CREATE INDEX [IX_FK_NHAN_VIEN_BANG_CAP]
ON [dbo].[NHAN_VIENList]
    ([LoaiBangCap]);
GO

-- Creating foreign key on [BoPhan] in table 'NHAN_VIENList'
ALTER TABLE [dbo].[NHAN_VIENList]
ADD CONSTRAINT [FK_NHAN_VIEN_BO_PHAN]
    FOREIGN KEY ([BoPhan])
    REFERENCES [dbo].[BO_PHANList]
        ([MaBoPhan])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_NHAN_VIEN_BO_PHAN'
CREATE INDEX [IX_FK_NHAN_VIEN_BO_PHAN]
ON [dbo].[NHAN_VIENList]
    ([BoPhan]);
GO

-- Creating foreign key on [ChucVu] in table 'NHAN_VIENList'
ALTER TABLE [dbo].[NHAN_VIENList]
ADD CONSTRAINT [FK_NHAN_VIEN_CHUC_VU]
    FOREIGN KEY ([ChucVu])
    REFERENCES [dbo].[CHUC_VU]
        ([MaChucVu])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_NHAN_VIEN_CHUC_VU'
CREATE INDEX [IX_FK_NHAN_VIEN_CHUC_VU]
ON [dbo].[NHAN_VIENList]
    ([ChucVu]);
GO

-- Creating foreign key on [MaPhieuMuon] in table 'CT_PHIEUMUONList'
ALTER TABLE [dbo].[CT_PHIEUMUONList]
ADD CONSTRAINT [FK_CT_PHIEUMUON_PHIEU_MUON]
    FOREIGN KEY ([MaPhieuMuon])
    REFERENCES [dbo].[PHIEU_MUONList]
        ([MaPhieuMuon])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CT_PHIEUMUON_PHIEU_MUON'
CREATE INDEX [IX_FK_CT_PHIEUMUON_PHIEU_MUON]
ON [dbo].[CT_PHIEUMUONList]
    ([MaPhieuMuon]);
GO

-- Creating foreign key on [MaSach] in table 'CT_PHIEUMUONList'
ALTER TABLE [dbo].[CT_PHIEUMUONList]
ADD CONSTRAINT [FK_CT_PHIEUMUON_SACH]
    FOREIGN KEY ([MaSach])
    REFERENCES [dbo].[SACHList]
        ([MaSach])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CT_PHIEUMUON_SACH'
CREATE INDEX [IX_FK_CT_PHIEUMUON_SACH]
ON [dbo].[CT_PHIEUMUONList]
    ([MaSach]);
GO

-- Creating foreign key on [MaCT_PhieuMuon] in table 'CT_PHIEUTRAList'
ALTER TABLE [dbo].[CT_PHIEUTRAList]
ADD CONSTRAINT [FK_CT_PHIEUTRA_CT_PHIEUMUON]
    FOREIGN KEY ([MaCT_PhieuMuon])
    REFERENCES [dbo].[CT_PHIEUMUONList]
        ([MaCT_PhieuMuon])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CT_PHIEUTRA_CT_PHIEUMUON'
CREATE INDEX [IX_FK_CT_PHIEUTRA_CT_PHIEUMUON]
ON [dbo].[CT_PHIEUTRAList]
    ([MaCT_PhieuMuon]);
GO

-- Creating foreign key on [MaPhieuTra] in table 'CT_PHIEUTRAList'
ALTER TABLE [dbo].[CT_PHIEUTRAList]
ADD CONSTRAINT [FK_CT_PHIEUTRA_PHIEU_TRA]
    FOREIGN KEY ([MaPhieuTra])
    REFERENCES [dbo].[PHIEU_TRAList]
        ([MaPhieuTra])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CT_PHIEUTRA_PHIEU_TRA'
CREATE INDEX [IX_FK_CT_PHIEUTRA_PHIEU_TRA]
ON [dbo].[CT_PHIEUTRAList]
    ([MaPhieuTra]);
GO

-- Creating foreign key on [MaLyDo] in table 'CT_THANHLY'
ALTER TABLE [dbo].[CT_THANHLY]
ADD CONSTRAINT [FK_CT_THANHLY_LYDO_THANHLY]
    FOREIGN KEY ([MaLyDo])
    REFERENCES [dbo].[LYDO_THANHLY]
        ([MaLyDo])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CT_THANHLY_LYDO_THANHLY'
CREATE INDEX [IX_FK_CT_THANHLY_LYDO_THANHLY]
ON [dbo].[CT_THANHLY]
    ([MaLyDo]);
GO

-- Creating foreign key on [MaSach] in table 'CT_THANHLY'
ALTER TABLE [dbo].[CT_THANHLY]
ADD CONSTRAINT [FK_CT_THANHLY_SACH]
    FOREIGN KEY ([MaSach])
    REFERENCES [dbo].[SACHList]
        ([MaSach])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CT_THANHLY_SACH'
CREATE INDEX [IX_FK_CT_THANHLY_SACH]
ON [dbo].[CT_THANHLY]
    ([MaSach]);
GO

-- Creating foreign key on [MaPhieuThanhLy] in table 'CT_THANHLY'
ALTER TABLE [dbo].[CT_THANHLY]
ADD CONSTRAINT [FK_CT_THANHLY_THANH_LY_SACH]
    FOREIGN KEY ([MaPhieuThanhLy])
    REFERENCES [dbo].[THANH_LY_SACH]
        ([MaPhieuThanhLy])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CT_THANHLY_THANH_LY_SACH'
CREATE INDEX [IX_FK_CT_THANHLY_THANH_LY_SACH]
ON [dbo].[CT_THANHLY]
    ([MaPhieuThanhLy]);
GO

-- Creating foreign key on [MaLoaiDocGia] in table 'DOC_GIAList'
ALTER TABLE [dbo].[DOC_GIAList]
ADD CONSTRAINT [FK_DOC_GIA_LOAI_DOCGIA]
    FOREIGN KEY ([MaLoaiDocGia])
    REFERENCES [dbo].[LOAI_DOCGIAList]
        ([MaLoaiDocGia])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DOC_GIA_LOAI_DOCGIA'
CREATE INDEX [IX_FK_DOC_GIA_LOAI_DOCGIA]
ON [dbo].[DOC_GIAList]
    ([MaLoaiDocGia]);
GO

-- Creating foreign key on [NguoiLap] in table 'DOC_GIAList'
ALTER TABLE [dbo].[DOC_GIAList]
ADD CONSTRAINT [FK_DOC_GIA_NHAN_VIEN]
    FOREIGN KEY ([NguoiLap])
    REFERENCES [dbo].[NHAN_VIENList]
        ([MaNV])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DOC_GIA_NHAN_VIEN'
CREATE INDEX [IX_FK_DOC_GIA_NHAN_VIEN]
ON [dbo].[DOC_GIAList]
    ([NguoiLap]);
GO

-- Creating foreign key on [MaTheDocGia] in table 'PHIEU_MUONList'
ALTER TABLE [dbo].[PHIEU_MUONList]
ADD CONSTRAINT [FK_PHIEU_MUON_DOC_GIA]
    FOREIGN KEY ([MaTheDocGia])
    REFERENCES [dbo].[DOC_GIAList]
        ([MaTheDocGia])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PHIEU_MUON_DOC_GIA'
CREATE INDEX [IX_FK_PHIEU_MUON_DOC_GIA]
ON [dbo].[PHIEU_MUONList]
    ([MaTheDocGia]);
GO

-- Creating foreign key on [MaTheDocGia] in table 'PHIEU_TRAList'
ALTER TABLE [dbo].[PHIEU_TRAList]
ADD CONSTRAINT [FK_PHIEU_TRA_DOC_GIA]
    FOREIGN KEY ([MaTheDocGia])
    REFERENCES [dbo].[DOC_GIAList]
        ([MaTheDocGia])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PHIEU_TRA_DOC_GIA'
CREATE INDEX [IX_FK_PHIEU_TRA_DOC_GIA]
ON [dbo].[PHIEU_TRAList]
    ([MaTheDocGia]);
GO

-- Creating foreign key on [MaTheDocGia] in table 'PHIEUGHI_MATSACH'
ALTER TABLE [dbo].[PHIEUGHI_MATSACH]
ADD CONSTRAINT [FK_PHIEUGHI_MATSACH_DOC_GIA]
    FOREIGN KEY ([MaTheDocGia])
    REFERENCES [dbo].[DOC_GIAList]
        ([MaTheDocGia])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PHIEUGHI_MATSACH_DOC_GIA'
CREATE INDEX [IX_FK_PHIEUGHI_MATSACH_DOC_GIA]
ON [dbo].[PHIEUGHI_MATSACH]
    ([MaTheDocGia]);
GO

-- Creating foreign key on [MaTheDocGia] in table 'PHIEUTHU_TIENPHAT'
ALTER TABLE [dbo].[PHIEUTHU_TIENPHAT]
ADD CONSTRAINT [FK_PHIEUTHU_TIENPHAT_DOC_GIA]
    FOREIGN KEY ([MaTheDocGia])
    REFERENCES [dbo].[DOC_GIAList]
        ([MaTheDocGia])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PHIEUTHU_TIENPHAT_DOC_GIA'
CREATE INDEX [IX_FK_PHIEUTHU_TIENPHAT_DOC_GIA]
ON [dbo].[PHIEUTHU_TIENPHAT]
    ([MaTheDocGia]);
GO

-- Creating foreign key on [NhaXuatBan] in table 'SACHList'
ALTER TABLE [dbo].[SACHList]
ADD CONSTRAINT [FK_SACH_NHA_XUAT_BAN]
    FOREIGN KEY ([NhaXuatBan])
    REFERENCES [dbo].[NHA_XUAT_BAN]
        ([MaNhaXuatBan])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SACH_NHA_XUAT_BAN'
CREATE INDEX [IX_FK_SACH_NHA_XUAT_BAN]
ON [dbo].[SACHList]
    ([NhaXuatBan]);
GO

-- Creating foreign key on [MaNV] in table 'PHIEUGHI_MATSACH'
ALTER TABLE [dbo].[PHIEUGHI_MATSACH]
ADD CONSTRAINT [FK_PHIEUGHI_MATSACH_NHAN_VIEN]
    FOREIGN KEY ([MaNV])
    REFERENCES [dbo].[NHAN_VIENList]
        ([MaNV])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PHIEUGHI_MATSACH_NHAN_VIEN'
CREATE INDEX [IX_FK_PHIEUGHI_MATSACH_NHAN_VIEN]
ON [dbo].[PHIEUGHI_MATSACH]
    ([MaNV]);
GO

-- Creating foreign key on [MaNhanVien] in table 'PHIEUTHU_TIENPHAT'
ALTER TABLE [dbo].[PHIEUTHU_TIENPHAT]
ADD CONSTRAINT [FK_PHIEUTHU_TIENPHAT_NHAN_VIEN]
    FOREIGN KEY ([MaNhanVien])
    REFERENCES [dbo].[NHAN_VIENList]
        ([MaNV])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PHIEUTHU_TIENPHAT_NHAN_VIEN'
CREATE INDEX [IX_FK_PHIEUTHU_TIENPHAT_NHAN_VIEN]
ON [dbo].[PHIEUTHU_TIENPHAT]
    ([MaNhanVien]);
GO

-- Creating foreign key on [NguoiTiepNhan] in table 'SACHList'
ALTER TABLE [dbo].[SACHList]
ADD CONSTRAINT [FK_SACH_NHAN_VIEN]
    FOREIGN KEY ([NguoiTiepNhan])
    REFERENCES [dbo].[NHAN_VIENList]
        ([MaNV])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SACH_NHAN_VIEN'
CREATE INDEX [IX_FK_SACH_NHAN_VIEN]
ON [dbo].[SACHList]
    ([NguoiTiepNhan]);
GO

-- Creating foreign key on [NguoiThanhLy] in table 'THANH_LY_SACH'
ALTER TABLE [dbo].[THANH_LY_SACH]
ADD CONSTRAINT [FK_THANH_LY_SACH_NHAN_VIEN]
    FOREIGN KEY ([NguoiThanhLy])
    REFERENCES [dbo].[NHAN_VIENList]
        ([MaNV])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_THANH_LY_SACH_NHAN_VIEN'
CREATE INDEX [IX_FK_THANH_LY_SACH_NHAN_VIEN]
ON [dbo].[THANH_LY_SACH]
    ([NguoiThanhLy]);
GO

-- Creating foreign key on [MaSach] in table 'PHIEUGHI_MATSACH'
ALTER TABLE [dbo].[PHIEUGHI_MATSACH]
ADD CONSTRAINT [FK_PHIEUGHI_MATSACH_SACH]
    FOREIGN KEY ([MaSach])
    REFERENCES [dbo].[SACHList]
        ([MaSach])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PHIEUGHI_MATSACH_SACH'
CREATE INDEX [IX_FK_PHIEUGHI_MATSACH_SACH]
ON [dbo].[PHIEUGHI_MATSACH]
    ([MaSach]);
GO

-- Creating foreign key on [MaTacGia] in table 'SACHList'
ALTER TABLE [dbo].[SACHList]
ADD CONSTRAINT [FK_SACH_TAC_GIA]
    FOREIGN KEY ([MaTacGia])
    REFERENCES [dbo].[TAC_GIAList]
        ([MaTacGia])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SACH_TAC_GIA'
CREATE INDEX [IX_FK_SACH_TAC_GIA]
ON [dbo].[SACHList]
    ([MaTacGia]);
GO

-- Creating foreign key on [MaTheLoai] in table 'SACHList'
ALTER TABLE [dbo].[SACHList]
ADD CONSTRAINT [FK_SACH_THE_LOAI]
    FOREIGN KEY ([MaTheLoai])
    REFERENCES [dbo].[THE_LOAIList]
        ([MaTheLoai])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SACH_THE_LOAI'
CREATE INDEX [IX_FK_SACH_THE_LOAI]
ON [dbo].[SACHList]
    ([MaTheLoai]);
GO

-- Creating foreign key on [TinhTrangSach] in table 'SACHList'
ALTER TABLE [dbo].[SACHList]
ADD CONSTRAINT [FK_SACH_TINH_TRANG]
    FOREIGN KEY ([TinhTrangSach])
    REFERENCES [dbo].[TINH_TRANG]
        ([MaTinhTrang])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SACH_TINH_TRANG'
CREATE INDEX [IX_FK_SACH_TINH_TRANG]
ON [dbo].[SACHList]
    ([TinhTrangSach]);
GO

-- Creating foreign key on [TenDangNhap] in table 'USERs'
ALTER TABLE [dbo].[USERs]
ADD CONSTRAINT [FK_USER_NHAN_VIEN]
    FOREIGN KEY ([TenDangNhap])
    REFERENCES [dbo].[NHAN_VIENList]
        ([MaNV])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_USER_NHAN_VIEN'
CREATE INDEX [IX_FK_USER_NHAN_VIEN]
ON [dbo].[USERs]
    ([TenDangNhap]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------